let cartData={
    aCartData:localStorage['aCartData']!==undefined?JSON.parse(localStorage['aCartData']):[],
    total:localStorage['total']!==undefined?parseFloat(localStorage['total']):0,
    freight: localStorage['freight']!==undefined?parseFloat(localStorage['freight']):0,
}
export default  function redCart(state=cartData,action) {
    switch (action.type) {
        case 'addcart':
            addCart(state,action.action.data)
            return Object.assign({},state,action.data);
        case 'delectCart':
            delectCart(state,action.action.data)
            return Object.assign({},state,action.data);
        case 'checkedItem':
            checkedItem(state,action.action.data)
            return Object.assign({},state,action.data);
        case 'clickAllChecked':
            clickAllChecked(state,action.action.data)
            return Object.assign({},state,action.data);
        case 'incAmount':
            incAmount(state,action.action.data);
            return Object.assign({},state,action.data);
        case 'decAmount':
            decAmount(state,action.action.data);
            return Object.assign({},state,action.data);
        case 'changeAmount':
            changeAmount(state,action.action.data)
            return Object.assign({},state,action.data)
        case 'clearCart':
            redClearCart(state)
            return Object.assign({},state,action.data)
        default:
            return state
    }
    //添加购物车
    function addCart(state,action) {
        //购物车里相同的商品属性，数量合加
        let bSameItem=false;
        if(state.aCartData.length>0){
            for (let i in state.aCartData){
                if(state.aCartData[i].gid===action.gid && JSON.stringify(state.aCartData[i].attrs)===JSON.stringify(action.attrs)){
                    state.aCartData[i].amount+=action.amount;
                    bSameItem=true;
                }

            }
        }
        //购物车里没有相同的商品属性，添加到购物车
        if (!bSameItem){
            state.aCartData.push(action);
        }
        computes(state);
        localStorage['aCartData']=JSON.stringify(state.aCartData)
    }
    //计算价格
    function computes(state) {
        let  total=0;
        //购物车商品总价
        for (let i in state.aCartData) {
            if(!state.aCartData[i].checked){
                total+=parseFloat(state.aCartData[i].price)*parseInt(state.aCartData[i].amount);
            }
        }
        state.total=parseFloat(Math.round(total).toFixed(2));
        localStorage['total']=state.total;
        freight(state)
    }
    //计算运费
    function freight(state) {
        let aFreigjt=[];
        if (state.aCartData.length>0){
            for (let key in state.aCartData){
                if(!state.aCartData[key].checked){
                    aFreigjt.push(state.aCartData[key].freight)
                }
            }
        }
        state.freight=Math.max.apply(null,aFreigjt);
        localStorage['freight']=state.freight
    }
    //删除购物车
    function delectCart(state,action) {
        if(state.aCartData.length>0){
            state.aCartData.splice(action.index,1)
        };
        computes(state);
        localStorage['aCartData']=JSON.stringify(state.aCartData);
    }
    //选择产品
    function checkedItem(state,action){
        state.aCartData[action.index].checked=action.checked;
        computes(state);
        localStorage['aCartData']=JSON.stringify(state.aCartData);
    }
    //点击全选
    function clickAllChecked(state,action){
        if(state.aCartData.length>0){
            for (let i in state.aCartData){
                state.aCartData[i].checked=action.checked
            }
        }
        computes(state);
        localStorage['aCartData']=JSON.stringify(state.aCartData);
    }
    //增加数量
    function incAmount(state,action) {
        state.aCartData[action.index].amount+=1;
        computes(state);
        localStorage['aCartData']=JSON.stringify(state.aCartData);
    }
    //减少数量
    function decAmount(state,action) {
        state.aCartData[action.index].amount-=1;
        computes(state);
        localStorage['aCartData']=JSON.stringify(state.aCartData);
    }
    //改变数量
    function changeAmount(state,action) {
        state.aCartData[action.index].amount=action.amount;
    }
    //清除购物车
    function redClearCart(state) {
        localStorage.removeItem('aCartData');
        localStorage.removeItem('total');
        localStorage.removeItem('freight');
        localStorage.removeItem('aid')
        state.aCartData=[];
        state.total=0;
        state.freight=0;
    }


}